package housing.task2.db_exschange;

import housing.task1.actor.Address;
import housing.task2.factory.JSNFactory;
import housing.task2.serializator.AbstractWriter;
import housing.task3.dao.HsAddressDao;
import housing.task3.dto.HsAddress;
import housing.task3.exceptions.HsAddressDaoException;

import java.io.File;

/**
 * Exports DB Tables into .jsn file.
 * <p/>
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 03.12.2010
 * Time: 15:05:56
 */
public class JsnExporter {
    private File DefaultExportFile =
            new File(".\\timoshenko_housing\\src\\main\\resources\\test\\exportAddresses.jsn");

    /**
     * Should create HsAddressDao before exporting
     *
     * @param dao
     */
    public void export(HsAddressDao dao) {
        AbstractWriter jsnWriter =
                new JSNFactory().createWriter();

        jsnWriter.setAddressOut(DefaultExportFile);

        try {
            HsAddress[] result = dao.findAll();
            Address temp;

            for (HsAddress addr : result) {
                temp = new Address(
                        addr.getAddressId(),
                        addr.getStreet(),
                        addr.getHouse(),
                        addr.getFlat());


                jsnWriter.write(temp);
            }
        } catch (HsAddressDaoException e) {
            e.printStackTrace();
        }

    }

    public void export(HsAddressDao dao, File file) {
        setDefaultExportFile(file);
        this.export(dao);
    }

    public File getDefaultExportFile() {
        return DefaultExportFile;
    }

    public void setDefaultExportFile(File defaultExportFile) {
        DefaultExportFile = defaultExportFile;
    }
}
